package com.trigsoft.applist;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.trigsoft.applist.ExceptionCursorAdapter.Wrapper;

public class ExceptionListAct extends Activity implements OnItemClickListener, OnClickListener, RefreshActivity
{
	private abstract class InList
	{
		Cursor c;

		public void setView()
		{
			ExceptionListAct.this.startManagingCursor(c);
			//SimpleCursorAdapter sa = new SimpleCursorAdapter(this, R.layout.exception_item, 
			//		cursor, columns, to);
			//sa.setViewBinder(new ExceptionApplicationViewBinder());
			notInListView.setAdapter(new ExceptionCursorAdapter(ExceptionListAct.this, c));
		}

		abstract public void switchList();

		abstract public void update();
	}

	private class InProgramList extends InList
	{

		public InProgramList()
		{
			// TODO Auto-generated constructor stub
			setView();
		}

		@Override
		public void setView()
		{
			titleView.setText(R.string.appTitle);
			switchBtn.setText(R.string.recentBtn);
			;
			c = DBHelper.queryNotInList();
			super.setView();
		}

		@Override
		public void switchList()
		{
			update();
			ExceptionListAct.this.switchList(new InRecentList());
		}

		@Override
		public void update()
		{
			for (String pkg : itemsToRemove.keySet())
				DBHelper.updateInList(DBHelper.in, pkg);
		}
	}

	private class InRecentList extends InList
	{

		public InRecentList()
		{
			// TODO Auto-generated constructor stub
			setView();
		}

		@Override
		public void setView()
		{
			titleView.setText(R.string.recentTitle);
			switchBtn.setText(R.string.appBtn);
			;
			c = DBHelper.queryNotInRecent();
			super.setView();
		}

		@Override
		public void switchList()
		{
			update();
			ExceptionListAct.this.switchList(new InProgramList());
		}

		@Override
		public void update()
		{
			NotInRecent nir = new NotInRecent(ExceptionListAct.this);
			for (String name : itemsToRemove.keySet())
			{
				DBHelper.updateInRecent(DBHelper.in, name);
				nir.remove(itemsToRemove.get(name), name);
			}
			nir.save(ExceptionListAct.this);
		}
	}

	private TextView titleView;
	private Button switchBtn;

	private ListView notInListView;

	private InList inList;

	private HashMap<String, String> itemsToRemove = new HashMap<String, String>();

	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		default:
			inList.switchList();
			break;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.exceptionlist);
		notInListView = (ListView) findViewById(R.id.notinlist);
		notInListView.setOnItemClickListener(this);

		switchBtn = (Button) findViewById(R.id.switchBtn);
		switchBtn.setOnClickListener(this);
		titleView = (TextView) findViewById(R.id.title1);

		inList = new InRecentList();

		findViewById(R.id.exportButton).setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				new ExportTask(ExceptionListAct.this).execute();
			}
		});

		findViewById(R.id.importButton).setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				new ImportTask(ExceptionListAct.this).execute();
			}
		});
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
		// TODO Auto-generated method stub
		Wrapper wrapper = (Wrapper) arg1.getTag();
		wrapper.checked = !wrapper.checked;
		wrapper.text.setChecked(wrapper.checked);

		if (wrapper.checked)
		{
			itemsToRemove.put(wrapper.name, wrapper.pkg);
		} else
		{
			itemsToRemove.remove(wrapper.name);
		}
	}

	@Override
	protected void onPause()
	{
		// TODO Auto-generated method stub

		inList.update();
		super.onPause();
	}

	public void setView()
	{
		startActivity(new Intent(this, ExceptionListAct.class));
		this.finish();
	}

	public void switchList(InList inList)
	{
		this.inList = inList;
		//inList.setView();
	}
}
